﻿#include "ObjectManager.h"

#include <qdebug.h>

//ACT/Codec/MCUCodec
#include "MCUCodec.h"
#include "MCUCodec_SetInterval_MEMSData.h"
#include "MCUCodec_SetInterval_TurnTableState.h"
//ACT/Codec/TcpCodec
#include "TcpCodec.h"
#include "TcpCodec_HeartBeat.h"
#include "TcpCodec_ReadSensorData.h"
#include "TcpCodec_DataBase.h"
#include "TcpCodec_MCUCtl.h"
#include "TcpCodec_MEMSData.h"
#include "TcpCodec_TurnTable.h"
#include "TcpCodec_Register.h"
#include "TcpCodec_User.h"
#include "TcpCodec_Transmit.h"
#include "TcpCodec_Script.h"
//ACT/Codec/TurnTableCodec
#include "TurnTableCodec.h"
//ACT/Codec/UserRegisterCodec
#include "UserRegisterCodec.h"
//ACT/Codec
#include "ReadMEMSDataCodec_Client.h"
#include "UserInfoCodec.h"
//ACT/Manager
#include "HeartBeatManager.h"
#include "DeviceInfoManager.h"
//IO
#include "TcpIOer.h"
//UI
#include "MainWindow.h"
#include "DialogLogin.h"

#include "Dialog_SetThermostat.h"
#include "Dialog_SetLoadMode.h"
#include "Dialog_SetRunningMode.h"

#include "MemsAutoTestSoftware.h"
#include "SetPositionModeDialog.h"
#include "SetRateModeDialog.h"
#include "SetScriptDialog.h"
#include "SetScriptLoopDialog.h"
#include "SetScriptWaitDialog.h"

#include "MacroDefinition.h"

ObjectManager* ObjectManager::objectManager=Q_NULLPTR;

ObjectManager::ObjectManager(QObject *parent) : QObject(parent)
{
  objectManager=this;
  objectManager->setObjectName("objectManager");
  objectCreation();
  objectInjection();
  objectInitialization();
  logSaver->saveLog("ObjectManager created");
  getObjFromMap<DialogLogin>()->show();
//  getObjFromMap<MainWindow>()->show();
}

ObjectManager::~ObjectManager()
{
  for(QString objName : objMap.keys())
    {
      removeFromObjMap(objName);
    }
}

ObjectManager* ObjectManager::getObjectManager()
{
  return Q_NULLPTR==objectManager?objectManager=new ObjectManager():objectManager;
}

void ObjectManager::objectCreation()
{
  //ACT/Codec/MCUCodec
  addToObjMap<MCUCodec>();
  addToObjMap<MCUCodec_SetInterval_MEMSData>();
  addToObjMap<MCUCodec_SetInterval_TurnTableState>();
  addToObjMap<TcpCodec_Transmit>();
  //ACT/Codec/TcpCodec
  addToObjMap<TcpCodec>();
  addToObjMap<TcpCodec_HeartBeat>();
  addToObjMap<TcpCodec_ReadSensorData>();
  addToObjMap<TcpCodec_DataBase>();
  addToObjMap<TcpCodec_MCUCtl>();
  addToObjMap<TcpCodec_MEMSData>();
  addToObjMap<TcpCodec_TurnTable>();
  addToObjMap<TcpCodec_Register>();
  addToObjMap<TcpCodec_User>();
  addToObjMap<TcpCodec_Transmit>();
//  addToObjMap<TcpCodec_Script>();
  //ACT/Codec/TurnTableCodec
  addToObjMap<TurnTableCodec>();
  //ACT/Codec/UserRegisterCodec
  addToObjMap<UserRegisterCodec>();
  //ACT/Codec
  addToObjMap<ReadMEMSDataCodec_Client>();
  addToObjMap<UserInfoCodec>();
  //ACT/Manager
  addToObjMap<HeartBeatManager>();
  addToObjMap<DeviceInfoManager>();
  //IO
  addToObjMap<TcpIOer>();
  //UI
  addToObjMap<MainWindow>();
  addToObjMap<DialogLogin>();

  addToObjMap<Dialog_SetThermostat>();
  addToObjMap<Dialog_SetLoadMode>();
  addToObjMap<Dialog_SetRunningMode>();

  addToObjMap<MemsAutoTestSoftware>();

//  addToObjMap<ParameterConsole>();
//  addToObjMap<SetMEMSDataDialog>();
//  addToObjMap<SetTurnTableAndThermostatDialog>();
//  addToObjMap<SetScriptDialog>();
//  addToObjMap<SetPositionModeDialog>();
//  addToObjMap<SetRateModeDialog>();
//  addToObjMap<SetScriptLoopDialog>();
//  addToObjMap<SetScriptWaitDialog>();
//  addToObjMap<SetThermostatDialog>();
}

#ifdef OBJECTINJECTION_ON
void ObjectManager::objectInjection()
{
  for(QObject* obj:objMap.values())
    {
      ObjAutoInject* temp=dynamic_cast<ObjAutoInject*>(obj);
      if(temp)
        {
          temp->objectAutoInjection(&objMap);
        }
    }
}
#else
void ObjectManager::objectInjection()
{}
#endif

void ObjectManager::objectInitialization()
{
}

void ObjectManager::removeFromObjMap(QObject* obj)
{
  if(Q_NULLPTR==obj)
    {
      return;
    }
  removeFromObjMap(obj->objectName());
}

void ObjectManager::removeFromObjMap(QString objName)
{
  QString name=objName;
  if(name.isEmpty())
    {
      logSaver->saveLog(QString("removeFromObjMap , obj name is empty"));
      return;
    }
  if(!objMap.contains(name))
    {
      logSaver->saveLog(QString("removeFromObjMap , obj %1 is not in objMap").arg(name));
      return;
    }
  objMap.take(name)->deleteLater();
  logSaver->saveLog(QString("removeFromObjMap , obj %1 is removed from objMap").arg(name));
}

void ObjectManager::addToObjMap(QObject* obj,QString objName)
{
  if(Q_NULLPTR==obj)
    {
      logSaver->saveLog("addToObjMap , obj is null");
      return;
    }
  obj->setObjectName(objName);
  QString name=obj->objectName();
  if(name.isEmpty())
    {
      logSaver->saveLog(QString("addToObjMap , obj name is empty"));
      obj->deleteLater();
      return;
    }
  if(objMap.contains(name))
    {
      logSaver->saveLog(QString("addToObjMap , obj %1 is already in objMap").arg(name));

      obj->deleteLater();
      return;
    }
  objMap.insert(name,obj);
  logSaver->saveLog(QString("addToObjMap , obj %1 is added to objMap").arg(name));
}







